package com.pcg.posgasstation.utils;

import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Base64;

import java.io.File;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

    private static final String TAG = Utils.class.getSimpleName();

    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    public static String[] getBirthdayArray(String birthday) {
        if (TextUtils.isEmpty(birthday)) {
            return null;
        }

        birthday = birthday.trim();
        if (TextUtils.isEmpty(birthday)) {
            return null;
        }

        birthday = birthday.replaceAll(" ", "");
        if (TextUtils.isEmpty(birthday)) {
            return null;
        }

        String[] birthdayArray = birthday.split("-");
        if (birthdayArray != null && birthdayArray.length == 3) {
            return birthdayArray;
        }

        return null;
    }


    private static final int MIN_DELAY_TIME = 500;  // 两次点击间隔不能少于1000ms
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }

    public static String saveBitmap(final Bitmap bitmap, final String filename) {
        final String root = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Face";
        final File myDir = new File(root);

        if (!myDir.exists() && !myDir.mkdirs()) {
            LogUtil.e(TAG, "saveBitmap: failed to mkdir on sdcard!");
        }

        final String fname = filename;
        final File file = new File(myDir, fname);
        if (file.exists()) {
            file.delete();
        }
        try {
            final FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 70, out);
            out.flush();
            out.close();
            return file.getAbsolutePath();
        } catch (final Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final String ENCRYPTION_KEY = "Seychjm";

    public static String encryptionPassword(String password) {

        if (TextUtils.isEmpty(password)) {
            return "";
        }
        byte[] bytes = password.getBytes();
        if (bytes == null) {
            return "";
        }
        String base64Str = Base64.encodeToString(bytes, Base64.DEFAULT);

//        LogUtil.e(TAG,"base64Str = " + base64Str);

        if (TextUtils.isEmpty(base64Str)) {
            return "";
        }
        String[] base64Array = base64Str.split("(?!^)");
        int strCount = base64Array.length;

        String key = ENCRYPTION_KEY;
        String[] keyArray = key.split("(?!^)");

//        LogUtil.e(TAG,"keyArray.length = " + keyArray.length);

        for (int i = 0; i < keyArray.length; i++) {
            String itemStr = keyArray[i];

//            LogUtil.e(TAG, "item key index " + i + " is " + itemStr);

            if (i < strCount) {
                base64Array[i] = base64Array[i] + itemStr;
            }
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (String item : base64Array) {
            stringBuilder.append(item);
        }

        String encryptionStr = stringBuilder.toString();

        encryptionStr = encryptionStr.replaceAll("=","O0O0O");
        encryptionStr = encryptionStr.replaceAll("\\+","o000o");
        encryptionStr = encryptionStr.replaceAll("/","oo00o");

//        LogUtil.e(TAG,"encryptionStr = " + encryptionStr);
        return encryptionStr;
    }

}
